﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：机构管理
 */
var refreshGirdData; // 更新数据
var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({ keyword: keyword });
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                selectedRow = null;
                learun.layerForm({
                    id: 'Form',
                    title: '添加机构',
                    url: config.webportal_server + 'pages/system/company/form.html?access_token=' + config.getToken().access_token,
                    width: 750,
                    height: 500,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'Form',
                        title: '编辑机构',
                        url: config.webportal_server + 'pages/system/company/form.html?access_token=' + config.getToken().access_token,
                        width: 750,
                        height: 500,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'companys/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 数据导出
            $('#lr_export').on('click', function () {
                location.href = config.base_server() + 'companyexcel/export?access_token=' + config.getToken().access_token;
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'companys?access_token=' + config.getToken().access_token,
                headData: [
                    { label: "机构名称", name: "fullName", width: 260, align: "left" },
                    { label: "机构编码", name: "code", width: 150, align: "left" },
                    { label: "机构简称", name: "shortName", width: 150, align: "left" },
                    { label: "负责人", name: "manager", width: 80, align: "center"},
                    {
                        label: "有效", name: "status", width: 50, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                        }
                    },
                    {
                      label: "更新时间", name: "updateTime", width: 140, align: "center",
                      formatter: function (value) {
                        return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                      }
                    },
                    { label: "备注", name: "description", width: 200, align: "left" },
                ],
                isTree: true,
                mainId: 'id',
                parentId: 'parentId'
            });
            page.search();
        },
        search: function (param) {
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}


